package com.by.controller;


import com.by.bean.Organize;
import com.by.common.bean.Page;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.ISave;

import com.by.service.OrganizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author HY
 * @version 1.0
 * @description
 * @date 2025/2/15
 * @since JDK21
 */
@RestController
@RequestMapping("/api/organize")
@Api(tags = "部门管理")

public class OrganizeController extends BaseController {

    @Autowired
    OrganizeService organizeService;

    @ApiOperation("部门查询")
    @GetMapping("/query")
    public ResultBean<List<Organize>> get() {
        List<Organize> query = organizeService.query();
        return query.size()!=0 ? new ResultBean(200,"查询成功",query) : new ResultBean(0,"查询失败",null);
    }

    @GetMapping("/queryByName/{name}")
    @ApiOperation("根据部门名称查询部门")
    public ResultBean<List<Organize>> queryByName(@PathVariable("name") String name) {
        List<Map> query = organizeService.query(name);
        return query.size()!=0 ? new ResultBean(200,"查询成功",query) : new ResultBean(0,"查询失败",null);
    }


    @PostMapping("/save")
    @ApiOperation("新增部门")
    public ResultBean saveDepartment(@Validated(ISave.class) @RequestBody Organize organize) {
        int save = organizeService.save(organize);

        return save!=0?success("新增部门成功"):fail("新增部门失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除部门")
    public ResultBean deleteDepartment(@PathVariable("id") int id) {
        int delete = organizeService.delete(id);
        return delete!=0?success("删除部门成功"):fail("删除部门失败");
    }
    @PutMapping("/update")
    @ApiOperation("修改部门")
    public ResultBean updateDepartment(@Validated(ISave.class) @RequestBody Organize organize) {
        int update = organizeService.update(organize);

        return update!=0?success("修改部门成功"):fail("修改部门失败");
    }

    @GetMapping("/queryNameByID/{id}")
    @ApiOperation("根据部门id查询部门名称")
    public List<Map> queryNameByID(@PathVariable(value = "id" ,required = false) int id) {
        List<Map> list = organizeService.queryNameByID(id);
        return list;
    }



}
